var changeVm = new Vue({
	el:'#equip-change-content',
	data: function() {
		return {
			equipmentid: '',
			zcbh: '',
			tmbh:'',
			zybbh:'',
			type: '',
			name:'',
			vendor: '',
			model: '',
			depart:'',
			workshop:'',
			user: '',
			contact:'',
			location:'',
			desc: ''
		}
	},
	mounted: function() {
		var self = this;
		mui.init();
		mui.plusReady(function () {
			var lastPage = plus.webview.getWebviewById('equipManage');
			if(lastPage) lastPage.close();
			self.initLayout();
			self.initDepartData();
			self.initWorkshopData();
			self.initData();
		})
	},
	methods:{
		initLayout:function() {
			mui('.mui-scroll-wrapper').scroll({
				scrollY: true, //是否竖向滚动
				scrollX: false, //是否横向滚动
				startX: 0, //初始化时滚动至x
				startY: 0, //初始化时滚动至y
				indicators: true, //是否显示滚动条
				deceleration: 0.0006, //阻尼系数,系数越小滑动越灵敏
				bounce: true //是否启用回弹
			})
		},
		initDepartData: function() {
			var self = this;
			this.departPicker = new mui.PopPicker();
			var url = config.requestBaseUrl + 'getSystemStandData';
			mui.ajax(url,{
			 	data:{
					type: 'depart'
			 	},
			 	dataType:'json',//服务器返回json格式数据
			 	type:'get',//HTTP请求类型
			 	timeout:3000,//超时时间设置为10秒；
			 	success:function(data){
					var departData = [];
					data.data.forEach(function(item, index) {
						departData.push({value: item.id, text: item.name})
					})
					self.departPicker.setData(departData);
			 	},
			 	error:function(xhr,type,errorThrown){
					mui.alert("出错了:" + errorThrown);
			 	}
			});
		},
		initWorkshopData: function() {
			var self = this;
			this.workshopPicker = new mui.PopPicker();
			var url = config.requestBaseUrl + 'getSystemStandData';
			mui.ajax(url,{
			 	data:{
					type: 'workshop'
			 	},
			 	dataType:'json',//服务器返回json格式数据
			 	type:'get',//HTTP请求类型
			 	timeout:3000,//超时时间设置为10秒；
			 	success:function(data){
					self.workshopSourceData = data.data;
			 	},
			 	error:function(xhr,type,errorThrown){
					mui.alert("出错了:" + errorThrown);
			 	}
			});
		},
		toDepartPicker: function() {
			var self = this;
			this.departPicker.show(function (selectItems) {
				self.depart = selectItems[0].text;
				self.departid = selectItems[0].value;
				// setTimeout(function() {
				// 	self.workshopDataConst();
				// }, 10);
			})
		},
		toWorkshopPicker:function() {
			this.workshopDataConst();
			var self = this;
			if(!this.departid) {
				mui.toast("请先选择部门!")
			} else {
				this.workshopPicker.show(function (selectItems) {
					self.workshop = selectItems[0].text;
					self.workshopid = selectItems[0].value;
				})	
			}
		},
		workshopDataConst: function() {
			var self = this;
			var workshopData = [];
			this.workshopSourceData.forEach(function(item, index) {
				if(item.id == self.departid) {
					workshopData.push({value: item.id, text: item.name});
				}
			})
			this.workshopPicker.setData(workshopData);
		},
		initData: function() {
			var self = this;
			var url = config.requestBaseUrl + 'getEquipmentBaseInfoById';
			
			var thisPage = plus.webview.currentWebview();
			this.equipmentid = thisPage.equipmentid;
			
			mui.ajax(url,{
				data:{
					equipmentid: this.equipmentid
				},
				dataType:'json',//服务器返回json格式数据
				type:'get',//HTTP请求类型
				timeout:10000,//超时时间设置为10秒；
				success:function(data){
					console.log(data.msg + data.isSuccess);
					if(data.isSuccess) {
						// self.equipmentid = data.data.id;
						self.tmbh = data.data.tmbh;
						self.zcbh = data.data.zcbh;
						self.zybbh = data.data.zybbh;
						self.type = data.data.type;
						self.name = data.data.name;
						self.vendor = data.data.vendor;
						self.model = data.data.model;
						self.depart = data.data.depart;
						self.departid = data.data.departid;
						self.workshop = data.data.workshop;
						self.workshopid = data.data.workshopid;
						self.user = data.data.user;
						self.contact = data.data.contact;
						self.location = data.data.location;
						self.desc = data.data.note;
						self.putindate = data.data.putindate;
						self.user = data.data.user;
						for(x in data.data) {
							console.log(x + ':' + data.data[x]);
						}
					} else {
						mui.toast(data.msg);
					}
				},
				error:function(xhr,type,errorThrown){
					mui.toast("出错啦:" + errorThrown);
				}
			});
		},
		saveChange: function() {
			var self = this;
			var url = config.requestBaseUrl + 'addChange';
			var recorderid = plus.storage.getItem('userid');
			var thisPage = plus.webview.currentWebview();
			
			mui.ajax(url,{
				data:{
					equipmentid: this.equipmentid,
					depart: this.depart,
					departid: this.departid,
					workshop: this.workshop,
					workshopid: this.workshopid,
					user: this.user,
					contact: this.contact,
					location: this.location,
					desc: this.desc,
					recorderid: recorderid
				},
				dataType:'json',//服务器返回json格式数据
				type:'post',//HTTP请求类型
				timeout:10000,//超时时间设置为10秒；
				success:function(data){
					mui.toast(data.msg);
					if(data.isSuccess) {
						mui.toast(data.msg);
						thisPage.close();
					}
				},
				error:function(xhr,type,errorThrown){
					console.log('失败' + errorThrown)
					mui.toast("出错!请重试或联系管理员");
				}
			});
		},
		scrapEquip: function() {
			var scrapPage = plus.webview.create('scrapEquipment.html', 'scrapEquipment',{
				left: '0%'
			}, {
				equipmentid: this.equipmentid
			});
			scrapPage.show({
				extras: {
					equipmentid: this.equipmentid
				}
			});
		}
	}
})